package org.example.edusmart.controller.Admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.edusmart.request.admin.UserInfoPojo;
import org.example.edusmart.service.UserService;
import org.example.edusmart.utils.PagerPojo;
import org.example.edusmart.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RequestMapping("/admin")
@RestController
public class GetAllUserInfoController {

    @Autowired
    private UserService userService;

    /**
     * 返回所有用户的部分参数
     *
     * @param userInfoPojo page 页码  size 一页多少条 username和number若传空字符串则返回所有用户信息 若传值则找特定用户信息
     * @return 分页后信息
     */
    @PostMapping("/getUserInfo/")
    @PreAuthorize("hasAuthority('admin_permission')") //指明有管理员权限的才能访问
    public Result getAllUserInfo(@RequestBody UserInfoPojo userInfoPojo) {

        PageHelper.startPage(userInfoPojo.getPage(), userInfoPojo.getSize());

        List<HashMap<String, Object>> userInfo = userService.allUserInfo(userInfoPojo);

        PageInfo<HashMap<String, Object>> pageInfo = new PageInfo<>(userInfo);
        PagerPojo pagerPojo = new PagerPojo(pageInfo.getTotal(), pageInfo.getList());

        return new Result(pagerPojo, 200, "成功");
    }
}
